package org.ipb.controller.modul.alamatpegawai;


import org.ipb.model.AlamatPegawai;
import org.ipb.model.Pegawai;

import util.hibernate.DAO;

import com.opensymphony.xwork2.ActionSupport;

public class ActionJQGridCrud extends ActionSupport {
	private static final long serialVersionUID = -5933849517817526086L;
	private String id;
	private String oper;
	private String pegawai;
	private String alamatRumah;
	private String kecamatan;
	private String kelurahan;
	private String kodePos;

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public String detail() {

		return SUCCESS;
	}

	public String page() {
		return "page";
	}

	public String execute() {
		if (getOper().equals("edit")) {
			return update();
		} else if (getOper().equals("add")) {
			return insert();
		} else {
			return delete();
		}
	}

	public String insert() {
		DAO<Integer> dao = new DAO<Integer>();
		AlamatPegawai object = new AlamatPegawai();
		Pegawai pegawai = new Pegawai();
		pegawai.setId(Integer.valueOf(getPegawai()));

		object.setPegawai(pegawai);
		object.setAlamatRumah(getAlamatRumah());
		object.setKecamatan(getKecamatan());
		object.setKodePos(getKodePos());
		object.setKelurahan(getKelurahan());

		dao.insert(object);
		dao.close();
		return SUCCESS;
	}

	public String update() {
		DAO<Integer> dao = new DAO<Integer>();
		AlamatPegawai object = (AlamatPegawai) dao.find(AlamatPegawai.class, Integer.valueOf(getId()));

		object.setAlamatRumah(getAlamatRumah());
		object.setKecamatan(getKecamatan());
		object.setKodePos(getKodePos());
		object.setKelurahan(getKelurahan());
		dao.update(object);
		dao.close();
		return SUCCESS;
	}

	public String delete() {
		DAO<Integer> dao = new DAO<Integer>();

		AlamatPegawai delete = (AlamatPegawai) dao.find(AlamatPegawai.class, Integer.valueOf(getId()));

		dao.delete(delete);
		dao.close();
		return SUCCESS;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAlamatRumah() {
		return alamatRumah;
	}

	public void setAlamatRumah(String alamatRumah) {
		this.alamatRumah = alamatRumah;
	}

	public String getKecamatan() {
		return kecamatan;
	}

	public void setKecamatan(String kecamatan) {
		this.kecamatan = kecamatan;
	}

	public String getKelurahan() {
		return kelurahan;
	}

	public void setKelurahan(String kelurahan) {
		this.kelurahan = kelurahan;
	}

	public String getKodePos() {
		return kodePos;
	}

	public void setKodePos(String kodePos) {
		this.kodePos = kodePos;
	}

	public String getPegawai() {
		return pegawai;
	}

	public void setPegawai(String pegawai) {
		this.pegawai = pegawai;
	}

}
